package com.example.yichuangyun.ui.fragment;

import android.os.Bundle;
import android.view.View;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.FragmentDetailsDataBinding;
import com.example.yichuangyun.dialog.MyDialog;
import com.example.yichuangyun.http.HttpRespond;
import com.example.yichuangyun.http.MyObserver;
import com.example.yichuangyun.http.RetrofitHelper;
import com.example.yichuangyun.model.EventBusModel;
import com.example.yichuangyun.model.business.NewAddBusinessModel;
import com.example.yichuangyun.model.business.ShareModel;
import com.example.yichuangyun.model.contacts.ContactPersonModel;
import com.example.yichuangyun.mvvm.base.BaseFragment;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.ui.adapter.DetailsContactAdapter;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.RequestUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 商机详情-资料详情
 */
public class DetailsDataFragment extends BaseFragment<FragmentDetailsDataBinding, BaseViewModel> {

    private final static String BUSINESS_ID = "businessId";
    private final static String BUSINESS_NAME = "businessName";
    private String businessId;
    private String mTelNumber;
    private String clientName;
    private List<ContactPersonModel> contactData = new ArrayList<>();
    private DetailsContactAdapter adapter;
    private MyDialog myDialog;
    private NewAddBusinessModel model;
    private boolean isOpen;

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void initData() {
        initDialog();
    }

    @Override
    public void lazyLoadData() {
        EventBus.getDefault().register(this);
        Bundle bundle = getArguments();
        if (bundle != null) {
            businessId = bundle.getString(BUSINESS_ID);
            clientName = bundle.getString(BUSINESS_NAME);
        }
        getInformationDetails();
    }

    private void getInformationDetails() {
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().getInformationDetail(businessId), new MyObserver<HttpRespond<NewAddBusinessModel>>() {
            @Override
            protected void onSuccess(HttpRespond<NewAddBusinessModel> newAddBusinessModelHttpRespond) {
                if (Constant.SUCCESS == newAddBusinessModelHttpRespond.code) {
                    model = newAddBusinessModelHttpRespond.data;
                    if (null != model) {
                        if (null != model.getSourceLists() && model.getSourceLists().size() > 0) {
                            binding.tvBusinessFrom.setText(model.getSourceLists().get(0).getSource());
                        }
                        binding.tvBusinessWeight.setText(CommonUtil.setText(model.getChanceWeight()));
                        binding.tvMoney.setText(mContext.getString(R.string.def_money, model.getExpectFree()));
                        binding.tvRemark.setText(CommonUtil.setText(model.getRemark()));
                        binding.tvOpen.post(() -> binding.tvOpen.setVisibility(binding.tvRemark.getLineCount() > 5 ? View.VISIBLE : View.GONE));
                        contactData = model.getAppTels();
                        adapter.onRefresh(contactData);
                        binding.tvBusinessId.setText(CommonUtil.setText(model.getId()));
                        binding.tvIntoTime.setText(CommonUtil.setText(model.getIntoChanceTime()));
                        binding.tvCreatePerson.setText(CommonUtil.setText(model.getFoundUser()));
                        binding.tvCreatePersonDept.setText(CommonUtil.setText(model.getFoundUserDept()));
                        binding.tvFollowPerson.setText(CommonUtil.setText(model.getGdUser()));
                        binding.tvFollowPersonDept.setText(CommonUtil.setText(model.getGdUserDept()));
                        if (null != model.getShareUsers() && model.getShareUsers().size() > 0) {
                            StringBuilder stringBuilder = new StringBuilder();
                            for (ShareModel shareModel : model.getShareUsers()) {
                                stringBuilder.append(shareModel.getShareUser()).append("，");
                            }
                            binding.tvSharePerson.setText(stringBuilder.substring(0, stringBuilder.toString().length() - 1));
                        }
                        binding.tvCreateTime.setText(CommonUtil.setText(model.getCreateTime()));
                        binding.tvUpdateTime.setText(CommonUtil.setText(model.getUpdateTime()));
                        binding.tvLastFollowTime.setText(CommonUtil.setText(model.getLastFollowTime()));
                        binding.tvNextFollowTime.setText(CommonUtil.setYHMTime(model.getNextFollowTime()));
                    }
                }
            }
        });
    }

    @Override
    public void initCtrl() {
        adapter = new DetailsContactAdapter(mContext, contactData);
        binding.recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initClick() {
        adapter.setOnCallListener(number -> {
            if (CommonUtil.isCanClick()) {
                mTelNumber = number;
                RequestUtils.checkCallNum(mContext, mTelNumber, myDialog, 0, true);
            }
        });

        binding.tvOpen.setOnClickListener(v -> {
            isOpen = !isOpen;
            if (isOpen) {
                binding.tvOpen.setText(getString(R.string.no_expand));
                binding.tvRemark.setMaxLines(Integer.MAX_VALUE);
            } else {
                binding.tvOpen.setText(getString(R.string.expand));
                binding.tvRemark.setMaxLines(5);
            }
        });
    }

    private void initDialog() {
        myDialog = new MyDialog(getActivity()) {
            @Override
            public void leftClick() {
                RequestUtils.callNum(mContext, mTelNumber);
                Constant.pushBusinessId = businessId;
                RequestUtils.addFollow(businessId, clientName, mTelNumber);
            }

            @Override
            public void rightClick() {
                RequestUtils.callbackOutbound(mContext, mTelNumber, businessId, clientName);
            }
        };
    }

    public static DetailsDataFragment newInstance(String businessId, String businessName) {
        Bundle bundle = new Bundle();
        bundle.putString(BUSINESS_ID, businessId);
        bundle.putString(BUSINESS_NAME, businessName);
        DetailsDataFragment detailsDataFragment = new DetailsDataFragment();
        detailsDataFragment.setArguments(bundle);
        return detailsDataFragment;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(EventBusModel eventBusModel) {
        if (null != eventBusModel) {
            if (eventBusModel.getMsg().equals(Constant.refreshInformationDetail)) {
                getInformationDetails();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
